package com.yunyao.framework.excel.imports;

import com.yunyao.common.core.BaseContext;
import com.yunyao.common.core.UserInfoRequest;
import com.yunyao.common.dto.excel.ContractMaterialExcelDto;
import com.yunyao.common.exception.BusinessException;
import com.yunyao.common.web.util.ConvertUtil;
import com.yunyao.dao.model.BamuMaterial;
import com.yunyao.dao.service.bamu.IBamuMaterialService;
import com.yunyao.framework.excel.ExcelImportHandle;
import com.yunyao.framework.excel.dto.ContractMaterialExcelImportDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * @author xingjishuai
 * @date 2024-07-24
 */
@Slf4j
@Component
public class ContractMaterialExcelImportHandle extends ExcelImportHandle<ContractMaterialExcelImportDto> {
    @Autowired
    private IBamuMaterialService iBamuMaterialService;

    public List<ContractMaterialExcelImportDto> importContractMaterialExcel(MultipartFile file) {
        try {
            List<ContractMaterialExcelImportDto> contractMaterialExcelImportDtoList =
                    importExcel(file, ContractMaterialExcelImportDto.class);

            UserInfoRequest userInfo = BaseContext.getUserInfo();
            contractMaterialExcelImportDtoList =
                    getExcelImportDtos(contractMaterialExcelImportDtoList);
            int i = 1;
            for (ContractMaterialExcelImportDto excelImportDto :
                    contractMaterialExcelImportDtoList) {

                String materialName = excelImportDto.getMaterialName();
                String materialSpecs = excelImportDto.getMaterialSpecs();
                if (StringUtils.isBlank(materialName) || StringUtils.isBlank(materialSpecs)) {
                    continue;
                }
                BamuMaterial bamuMaterial = iBamuMaterialService.getByNameAndSpecs(materialName,
                        materialSpecs);
                if (Objects.isNull(bamuMaterial)) {
                    String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern(
                            "yyyyMMdd"));
                    long incr = redisCache.incr("MATERIAL_CODE" + formattedDate);
                    bamuMaterial = new BamuMaterial();
                    bamuMaterial.setMaterialCode(formattedDate + String.format("%06d", incr));
                    bamuMaterial.setMaterialCategoryId(defaultMaterialCategoryId);
                    bamuMaterial.setMaterialCategoryCode(defaultMaterialCategoryCode);
                    bamuMaterial.setMaterialCategoryName(defaultMaterialCategoryName);
                    bamuMaterial.setMaterialName(materialName);
                    bamuMaterial.setMaterialSpecifications(materialSpecs);
                    bamuMaterial.setMaterialModel(excelImportDto.getMaterialModel());
                    bamuMaterial.setMaterialUnit(excelImportDto.getMaterialUnit());
                    bamuMaterial.setTenantId(userInfo.getTenantId());
                    iBamuMaterialService.save(bamuMaterial);
                    excelImportDto.setIsAdd(1);
                }
                BigDecimal includingTaxTotalPrice = excelImportDto.getIncludingTaxTotalPrice();
                BigDecimal materialNum = excelImportDto.getMaterialNum();
                BigDecimal tax = excelImportDto.getTax();
//                if (Objects.nonNull(includingTaxTotalPrice) && Objects.nonNull(materialNum) && Objects.nonNull(tax)) {
//                    BigDecimal includingTaxUnitPrice = includingTaxTotalPrice.divide(materialNum,
//                            4, RoundingMode.HALF_UP);
//                    excelImportDto.setExcludingTaxUnitPrice(includingTaxUnitPrice.subtract(includingTaxUnitPrice.multiply(tax.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP))));
//                    excelImportDto.setIncludingTaxUnitPrice(includingTaxUnitPrice);
//                }
                excelImportDto.setExcludingTaxUnitPrice(ConvertUtil.convertTaxTotal2UnitPrice(includingTaxTotalPrice, materialNum,tax).getExcludingTaxUnitPrice());
                excelImportDto.setIncludingTaxUnitPrice(ConvertUtil.convertTaxTotal2UnitPrice(includingTaxTotalPrice, materialNum,tax).getIncludingTaxUnitPrice());
                excelImportDto.setMaterialId(bamuMaterial.getId());
                excelImportDto.setMaterialCode(bamuMaterial.getMaterialCode());
                excelImportDto.setMaterialSortNum(i);
                i++;
            }
            return contractMaterialExcelImportDtoList;
        } catch (IOException e) {
            log.error("导入合同材料Excel文件失败", e);
            return List.of();
        }
    }

    private List<ContractMaterialExcelImportDto> getExcelImportDtos(List<ContractMaterialExcelImportDto> contractMaterialExcelImportDtoList) {
        if (CollectionUtils.isEmpty(contractMaterialExcelImportDtoList)) {
            throw new BusinessException("导入文件数据为空");
        }
        List<ContractMaterialExcelImportDto> list = Lists.newArrayList();
        for (ContractMaterialExcelImportDto importDto :
                contractMaterialExcelImportDtoList) {
            String materialName = importDto.getMaterialName();
            String materialSpecs = importDto.getMaterialSpecs();
            if (StringUtils.isBlank(materialName) || StringUtils.isBlank(materialSpecs)) {
                continue;
            }
            list.add(importDto);
        }
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("导入文件数据为空");
        }
        return list;
    }
}
